#include "object_detector.hpp"
#include <algorithm>
using namespace std;

_YOLO_NAMESPACE_BEGINE

float Box::area() const
{
    return std::max(right - left, 0.f) * std::max(bottom - top, 0.f);
}

Box Box::operator|(const Box &b) const
{
    Box res;
    res.left = std::min(left, b.left);
    res.top = std::min(top, b.top);
    res.right = std::max(right, b.right);
    res.bottom = std::max(bottom, b.bottom);
    return res;
}

Box Box::operator&(const Box &b) const
{
    Box res;
    res.left = std::max(left, b.left);
    res.top = std::max(top, b.top);
    res.right = std::min(right, b.right);
    res.bottom = std::min(bottom, b.bottom);
    return res;
}

float Box::iou(const Box &b) const
{
    Box inter = (*this) & b;
    float inter_area = inter.area();
    float uni_area = area() + b.area() - inter_area;
    if (inter_area == 0 || uni_area == 0)
        return 0.0f;
    return inter_area / uni_area;
}

_YOLO_NAMESPACE_END
